set conceallevel=1
let g:tex_flavor='latex'
let g:tex_conceal='abdmg'
let g:vimtex_delim_list = get(g:, 'vimtex#delim#lists', {})
let g:vimtex_mappings_enabled = 1
let g:vimtex_compiler_latexmk_executable = get(g:, 'vimtex_compiler_latexmk_executable', 'C:\Programs\texlive\2023\bin\windows\latexmk.exe')
if !executable(g:vimtex_compiler_latexmk_executable)
    let $PATH='C:\Program Files\texlive\2023\bin\windows;'.$PATH
    if executable('latexmk')
        let g:vimtex_compiler_latexmk_executable = exepath('latexmk')
    else
        if executable('C:\\Program\ Files\\texlive\\2023\\bin\\windows\\latexmk.exe')
            let g:vimtex_compiler_latexmk_executable = executable('latexmk') ? exepath('latexmk') : 'C:\\Program\ Files\\texlive\\2023\\bin\\windows\\latexmk.exe'
        endif
    endif
endif
let g:vimtex_view_general_viewer = get(g:, 'vimtex_view_general_viewer', 'SumatraPDF')
if !executable(g:vimtex_view_general_viewer)
    let $PATH='C:\Program Files\SumatraPDF;'.$PATH
    let g:vimtex_view_general_viewer = executable('SumatraPDF') ? exepath('SumatraPDF') : fnameescape("C:\\Program Files\\SumatraPDF\\SumatraPDF.exe")
endif


function! plugin#vimtex#load() abort
    if get(g:, 'vimtex_enabled', 1) 
        packadd vimtex
    endif
    return exists('g:loaded_vimtex') 
endfunction

function! plugin#vimtex#init() abort
    " latexrun
    if executable('latexrun')
        let g:vimtex_compiler_method = 'latexrun'
        let g:vimtex_compiler_latexrun_engines = {
                    \ '_'                : 'pdflatex',
                    \ 'pdflatex'         : 'pdflatex',
                    \ 'lualatex'         : 'lualatex',
                    \ 'xelatex'          : 'xelatex',
                    \}
    endif
    " latexmk
    if executable('latexmk') || executable(g:vimtex_compiler_latexmk_executable)
        let g:vimtex_compiler_method = 'latexmk'
        let g:vimtex_compiler_latexmk = {
                    \ 'aux_dir' : '',
                    \ 'out_dir' : '',
                    \ 'build_dir': 'pdf',
                    \ 'callback': 1,
                    \ 'continous': 1,
                    \ 'executable': g:vimtex_compiler_latexmk_executable,
                    \ 'hooks' : [],
                    \ 'options' : [
                    \   '-xelatex',
                    \   '-verbose',
                    \   '-file-line-error',
                    \   '-synctex=1',
                    \   '-interaction=nonstopmode',
                    \   '-aux-directory=pdf',
                    \   '-outdir=.',
                    \ ] ,
                    \}
        let g:vimtex_compiler_latexmk_engines = {
                    \ '_'                : '-pdf',
                    \ 'pdflatex'         : '-pdf',
                    \ 'dvipdfex'         : '-pdfdvi',
                    \ 'lualatex'         : '-lualatex',
                    \ 'xelatex'          : '-xelatex',
                    \ 'context (pdftex)' : '-pdf -pdflatex=texexec',
                    \ 'context (luatex)' : '-pdf -pdflatex=context',
                    \ 'context (xetex)'  : "-pdf -pdflatex='texexec --xtx'",
                    \}
    endif
    " viewer
    let g:vimtex_view_method='general'
    if executable('SumatraPDF')
        let g:vimtex_view_general_viewer = 'SumatraPDF'
    else
        let g:vimtex_view_general_viewer = 'C:\Programs\SumatraPDF\SumatraPDF.exe'
    endif
    " let g:vimtex_view_general_options_latexmk = '-reuse-instance'
    let g:vimtex_quickfix_open_on_warning=1
    let g:vimtex_quickfix_mode=1
    let g:vimtex_view_general_options
                \ = '-reuse-instance -forward-search @tex @line @pdf'
endfunction

